package qnb.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import qnb._common.easyui.EasyuiGridData;
import qnb._common.easyui.EasyuiPageVO;
import qnb._common.vo.MessageVOFactory;
import qnb.dao.TeacherDao;
import qnb.entity.Grade;
import qnb.entity.Teacher;
import qnb.service.GradeService;
import qnb.service.TeacherServiceDao;import java.lang.management.GarbageCollectorMXBean;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/grade")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @Autowired
    private TeacherServiceDao teacherService;

    @RequestMapping("/forward/gradeAdd")
    public String forwardTAdd() {
        return "grade/gradeAdd";
    }

    @RequestMapping("/forward/gradeList")
    public String forwardList() {
        return "grade/gradeList";
    }

//    //===================================== 上面的为跳转方法====================================

    /**
     *  分页查询全部
     * @param vo 分页条件
     * @return
     */
    @PostMapping("/selectAllGrade")
    @ResponseBody
    public Object selectAllGrade(EasyuiPageVO vo) {
        if (vo.isSqlError()) {
            System.out.println("======================");
            return null;
        }
        String whereSql = vo.getWhereSql();
        return EasyuiGridData.getEasyuiGridData(gradeService.selectCountByGrade(whereSql),
                gradeService.selectAllGrade(whereSql,vo.getOrderbySQL(),vo.getStart(),vo.getRows()));
    }




    /**
     *  绑定教师信息到下拉框中
     * @return
     */
    @PostMapping("/selectAllByTeacherName")
    @ResponseBody
    public List<Teacher> selectAllByTeacherName(){
//        List<Teacher> teacherList = teacherService.selectAll();
//        return teacherList;
    	
    	
    	//模拟老师数据
    	List<Teacher> list=new ArrayList<>();
		Teacher t;
		for(int i=0;i<50;i++) {
			t=new Teacher();
			t.setTeacherId(i);
			t.setTeacherName("名称"+i);
			list.add(t);
		}
		return list;
    }

    /**
     *  根据id查询指定信息并回显到页面
     * @param gradeId
     * @return
     */
    @RequestMapping("/selectGradeById")
    @ResponseBody  //异步请求
    public Grade selectGradeById(Integer gradeId) {
        if (null == gradeId || gradeId < 0) {
            return null;
        }
        return gradeService.selectGradeById(gradeId);
    }

    /**
     *  添加功能
     * @param grade
     * @return
     */
    @RequestMapping("/insertGrade")
    @ResponseBody
    public String insert(Grade grade) {
       return MessageVOFactory.getAddMessage(gradeService.insertGrade(grade));
    }

    /**
     * 修改班级信息
     * @param grade
     * @return
     */
    @PostMapping("/updateGrade")
    @ResponseBody
    public String updateGrade(Grade grade){
       return MessageVOFactory.getUpdateMessage(gradeService.updateGrade(grade));
    }

    /**
     *  删除功能
     * @param
     * @return
     */
    @RequestMapping("/deleteGrade")
    @ResponseBody
    public String deleteGrade(Integer gradeId){
        if (null == gradeId || gradeId < 0) {
            return null;
        }
        return MessageVOFactory.getDellMessage(gradeService.deleteGrade(gradeId));
    }


}
